<?php
if($_SESSION['group_id']!=3){redir("./");exit;}
$lesson=mysql_fetch_assoc(mysql_query("select * from lesson where lesson_id='$_GET[lesson_id]'"));
$ex=mysql_fetch_assoc(mysql_query("select count(*) as num from examination where lesson_id='$_GET[lesson_id]' and m_id='$_SESSION[m_id]'"));
$res=mysql_query("select * from course_regis where course_id='$lesson[course_id]' and m_id='$_SESSION[m_id]' and status_id=1");

if($lesson['exam_open']==0){
	redir("?mod=s.listlesson&course_id=$lesson[course_id]",5);
	die("<div id=error_msg>แบบทดสอบนี้ยังไม่เปิดให้ใช้งาน</div>");
}
if($ex['num']>0){redir("?mod=s.listlesson&course_id=$lesson[course_id]");exit;}
if(mysql_num_rows($res)==0){redir('./');exit;}
if(strtotime($lesson['exam_start'])>time()){
	redir("?mod=s.listlesson&course_id=$lesson[course_id]",5);
	die("<div id=error_msg>ยังไม่ถึงเวลาทำแบบทดสอบ<br>($lesson[exam_start] ถึง $lesson[exam_stop])</div>");
}
if(strtotime($lesson['exam_stop'])<time()){
	redir("?mod=s.listlesson&course_id=$lesson[course_id]",5);
	die("<div id=error_msg>หมดเวลาทำแบบทดสอบแล้ว<br>($lesson[exam_start] ถึง $lesson[exam_stop])</div>");
}
if($_POST['op']=='finish'){
	$ex_score=0;
	for($i=1;$i<=$lesson['question_num'];++$i){
		$choise=mysql_fetch_assoc(mysql_query("select choise_correct from choise where choise_id='".$_POST["choise_id$i"]."'"));
		$ex_score+=$choise['choise_correct'];
	}
	$ex_time=date('Y-m-d H:i:s');
	$ex_fullscore=$lesson['question_num'];
	$sql="insert into examination(ex_time,ex_score,ex_fullscore,lesson_id,m_id) values('$ex_time','$ex_score','$ex_fullscore','$_GET[lesson_id]','$_SESSION[m_id]')";
	mysql_query($sql)or die(mysql_error());
	redir("?mod=s.listlesson&course_id=$lesson[course_id]",5);
	die("<div id=com_msg>ส่งคำตอบเรียบร้อยแล้วกรุณารอสักครู่ <img src=images/load.gif></div>");
}
?>
<div class="box">
	<h2>ข้อสอบ : <?php echo $lesson['lesson_name'];?></h2>
	<?php if($err)echo "<div id=error_msg>$err</div>";?>
	<div class="boxContent">
		<p>
			<form method="post" enctype="multipart/form-data">
				<?php
				$choises='';
				if($lesson['exam_type']==0){
					$res1=mysql_query("select * from question where lesson_id='$_GET[lesson_id]' order by rand() limit $lesson[question_num]");
					$i=0;
					while($rs=mysql_fetch_assoc($res1)){
						$questions[]=$rs;
					}
				}else{
					$res_qs=mysql_query("select * from question_special where lesson_id='$_GET[lesson_id]'");
					while($qs=mysql_fetch_assoc($res_qs)){
						$resl=mysql_query("select * from question where lesson_id='$qs[master_lesson_id]' order by rand() limit $qs[qs_amount]");
						while($rs=mysql_fetch_assoc($resl)){
							$questions[]=$rs;
						}
					}
				}
				foreach($questions as $question){
					++$i;
					unset($choises);
				?>
				<table>
				<tr>
				<td width="500">
				<?php echo "$i. ".nl2br($question['question_detail']);?>
					<ul type="none">
						<?php
						$correct=mysql_fetch_assoc(mysql_query("select * from choise where question_id='$question[question_id]' and choise_correct = '1'"));
						$choises[]=$correct;
						$res2=mysql_query("select * from choise where question_id='$question[question_id]' and choise_id!='$correct[choise_id]' order by rand() limit 4");
						while($choise=mysql_fetch_assoc($res2)){
							$choises[]=$choise;
						}
						shuffle($choises);
						$j=0;
						foreach($choises as $choise){
							++$j;
						?>
						<li><input type="radio" name="choise_id<?php echo $i;?>" value="<?php echo $choise['choise_id'];?>" /> <?php echo $choise['choise_detail'];?></li>
						<?php
						}
						?>
					</ul>
				</td>
				<td>
					<?php echo ($question['question_pic']!='')?"<a href=questionpic/$question[question_pic] target=_blank rel=lytebox><img src=questionpic/$question[question_pic] height=150></a>":'&nbsp;';?>
				</td>
				</td>
				</tr>
				</table>
				<?php
				}
				?>
			<center><input type="submit" value="ส่งคำตอบ" onclick="return confirm('หากส่งคำตอบแล้วจะไม่สามารถเปลี่ยนแปลงได้อีก คุณต้องการส่งคำตอบหรือไม่?');" /><input type="hidden" name="op" value="finish" /></center>
			</form>
		</p>
	</div>
</div>
<script>
	function check_notice(lesson_id){
		var ran=Math.floor(Math.random()*100);
		$.ajax({
			type:"POST",
			url:"js/check_notice.php",
			data:ran + "&lesson_id=" + lesson_id,
			success:function(result){
				if(result<1){
					alert('หมดเวลาทำแบบทดสอบแล้ว');location.href='./';
				}
				else{
					if(result==900)alert('เหลือเวลาทำแบบทดสอบอีก 15 นาที');
					if(result==600)alert('เหลือเวลาทำแบบทดสอบอีก 10 นาที');
					if(result==300)alert('เหลือเวลาทำแบบทดสอบอีก 5 นาที');
					if(result==60)alert('เหลือเวลาทำแบบทดสอบอีก 1 นาที กรุณากดส่งคำตอบโดยด่วน !!!');
					setTimeout('check_notice('+lesson_id+')',500);
				}
			}
		});	
	}
$(document).ready(function() {

	setTimeout('check_notice(<?php echo $_GET['lesson_id'];?>)',500);
});
</script>